// const { request } = require("../../request");
import { request } from '../../request/index'
const md5 = require('../../crypto/md5.min.js')
var app = getApp()
// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    eyeFlag: true,
    form: {
      password: '',
      phone: '',
      salt:''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "登录"
    });
  },
  showToast(title) {
    wx.showToast({
      title: title,
      duration: 1500,
      icon: 'none',
    })
  },
  handleEyePassword() {
    this.setData({
      eyeFlag: !this.data.eyeFlag
    })
  },
  tapRegister(){
    wx.navigateTo({
      url:'/pages/register/register?name=register'
    })
  },
  forgetPassword(){
    wx.navigateTo({
      url:'/pages/register/register?name=forget'
    })
  },
  checkPhone(phone) {
    let str = /^1[3456789]\d{9}$/
    if (str.test(phone)) {
      return true
    }else{
      return false
    }
  },
  checkPassword(password) {
    let str = /^(?!bai[0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/
    if (str.test(password)) {
      return true
    }else{
      return false
    }
  },
  InputPhone(e){
    var name = e.currentTarget.dataset.name
    var form = this.data.form
    form[name] = e.detail.value
    this.setData({
      form
    })
  },
  login(){
    var phone = this.data.form.phone
    var password = this.data.form.password
 
    if(!this.checkPhone(phone)){
      return this.showToast('手机号码有误')
    }else if(!this.checkPassword(password)){
      return this.showToast('密码格式应由6-20字母和数字组成')
    }else{
      request({
        url:'/v1/salt',
        method:'post',
        data:{
          phone:parseInt(phone)
        }
      }).then(res=>{
        if(res.data.errCode==0){
          // this.setData({
          //   salt:res.data.data
          // })
          var salt = res.data.data
          var data = {
            phone:parseInt(phone),
            password:md5(md5(phone+md5(password))+salt),
            type:'c_applet'
          }
          request({
            url:'/v1/login',
            method:'post',
            data
          }).then(res=>{
            if (res.data.errCode == 0) {
              console.log(res);
              var userInfo = res.data.data
              var access_token = res.data.data.access_token
              var refresh_token = res.data.data.refresh_token
              this.showToast('登录成功')
              wx.setStorageSync("sirui_userInfo", userInfo);
              wx.setStorageSync("access_token", access_token);
              wx.setStorageSync("refresh_token", refresh_token);
              wx.setStorageSync('tokenTime',Date.parse(new Date()))
              app.data.isInit = 1;
              wx.navigateTo({
                url:'/pages/index/index'
              })
            } else {
              this.showToast(res.data.message)
            }
          })
        }else{
          this.showToast(res.data.message)
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {  
    // app.client.subscribe('/sirui/iot/shadow/5/MESHID:7844FDA3ED75/cmdStatusSynchronous/json', {
    //   qos: 0
    // }, function (err) {
    //   if (!err) {
    //     //client.publish('123', 'Hello mqtt')
    //     console.log("订阅成功")
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})